package com.byxy.mysystem.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.byxy.mysystem.model.system.SysUser;
import com.byxy.mysystem.service.system.ISysUserService;
import com.byxy.mysystem.util.JsonResult;
import com.byxy.mysystem.util.ResultTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author omd
 * @since 2022-09-23
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;

    @PostMapping("/list")
    public JsonResult<IPage<SysUser>> findByPage(@RequestBody Map<String,Object> params){
        long current = params.get("current") != null?Integer.valueOf(params.get("current").toString()):1;
        long size = params.get("size") != null?Integer.valueOf(params.get("size").toString()):10;
        IPage page = new Page(current,size);
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        if(params.get("userName") != null && StringUtils.isNotBlank(params.get("userName").toString())){
            wrapper.eq("userName",params.get("userName").toString());
        }
        IPage<SysUser> result = sysUserService.page(page, wrapper);
        return ResultTool.success(result);
    }

    @PostMapping("/find")
    public JsonResult<SysUser> find(Integer id){
        SysUser sysUser = sysUserService.getById(id);
        return ResultTool.success(sysUser);
    }

    @PostMapping("/update")
    public JsonResult update(@RequestBody SysUser sysUser){
        boolean flag = sysUserService.updateById(sysUser);
        return ResultTool.success(flag);
    }

    @PostMapping("/save")
    public JsonResult save(@RequestBody SysUser sysUser){
        boolean flag = sysUserService.save(sysUser);
        return ResultTool.success(flag);
    }

    @PostMapping("/delete")
    public JsonResult save(List<Integer> ids){
        boolean flag = sysUserService.removeByIds(ids);
        return ResultTool.success(flag);
    }


}
